﻿using Core.SimpleTemp.Application;
using Core.SimpleTemp.Application.ServiceApp.SysApp.DataDicApp;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.AutoMapperExtend;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.AutoMapperExtend.DataDictionary;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Mvc.Api.Sys
{
    /// <summary>
    /// 数据字典
    /// </summary>
    [ApiExplorerSettings(GroupName = "Sys")]
    [Route("api/[controller]")]
    [EnableCors("any")]
    [ApiController]
    [Authorize]

    public class DataDirController : CoreApiController
    {

        private readonly SysDataDicService _service;
        private readonly ILogger<DataDirController> _logger;
        private readonly IDataDicHelp _dataDicHelp;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="service"></param>
        /// <param name="logger"></param>
        /// <param name="dataDicHelp"></param>
        public DataDirController(SysDataDicService service, ILogger<DataDirController> logger, IDataDicHelp dataDicHelp)
        {
            _service = service;
            _logger = logger;
            _dataDicHelp = dataDicHelp;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetList")]
        [PermissionFilter(nameof(DataDirController) + "_" + nameof(DataDirController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetList()
        {
            var list = await _service.GetListAsync();
            //var list = await _service.GetListAsync(null, null, null, base);
            //  var ret=list.Select(a => new { a.Code, a.CreateFactory });
            return this.JsonSuccess(list);
        }

        /// <summary>
        /// 增/改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(DataDirController) + "_" + nameof(DataDirController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult> SaveAsync(SysDataDicDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(DataDirController) + "_" + nameof(DataDirController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult> DeleteMutiAsync([FromBody]Guid[] ids)
        {

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }

        /// <summary>
        /// 根据ID获取信息
        /// </summary>
        /// <param name="id">Guid</param>
        /// <returns></returns>
        [HttpGet("Get")]
        [PermissionFilter(nameof(DataDirController) + "_" + nameof(DataDirController.Get), AuthorizationConst.GET)]
        public async Task<IActionResult> Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            return this.JsonSuccess(model);
        }
        #endregion


        /// <summary>
        /// /根据一组字典类型category字段,返回多个以category作为KEY的集合
        /// </summary>
        /// <param name="categoryCodArray"></param>
        /// <returns></returns>
        [HttpGet("GetDicValue")]
        [PermissionFilter(nameof(DataDirController) + "_" + nameof(DataDirController.GeDataDicValueByCategory), AuthorizationConst.GET)]
        public IActionResult GeDataDicValueByCategory(string[] categoryCodArray)
        {
            var ret = _dataDicHelp.GeDataDicValueByCategory(categoryCodArray);
            return this.JsonSuccess(ret);
        }
    }
}